// pages/equipmenType/equipmenType.js
import CommonService from '../../utils/service/commonService.js';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Base64 from '../../utils/Base64.js';
const util = require('../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参来的设备id
    equipmentId:null, 
    //设备类型选择数据
    typeList:[],
    //一级分类列表
    typeOne:[],
    //设备类型选择框
    showType:false,   
    //选择的设备类型id
    typeIds: [],
    //选择的设备类型名字
    typeNames: ''   
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({
        equipmentId: parseInt(options.id),
      })
      
      //修改类型
      if(options.paramPartList){
        let obj=JSON.parse(options.paramPartList);        
        this.setData({
          typeIds: obj.ids,
          typeNames: obj.names,
        })
      }
    }     
  },   

  //查询设备一级分类
  findEquipmentCategory(){
    let param = {
      isAuth: false,
      className: 'equipmentCategory',
      data: {
        searchAll: true,
        fid: 1,
        selectFields: ["id", "fid", "name"],
        nbl:Base64.encode(util.timeFormatDay(new Date())),
      }
    }

    CommonService.goFindList(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        let cateArr=[];
        result.forEach(item => {
          cateArr.push({
            id:item.id,
            text:item.name,
          });
        });
        this.setData({
          typeOne: cateArr
        })
        //查询子分类
        this.findChildren(result[0].id);
      }
    }).catch((error) => {
    })
  },

  //查询子分类
  findChildren(fid){
    let param = {
      isAuth: false,
      className: 'equipmentCategory',
      data: {
        searchAll: true,
        fid: fid,
        selectFields: ["id", "fid", "name"],
        nbl:Base64.encode(util.timeFormatDay(new Date())),
      }
    }

    CommonService.goFindList(param).then(res=>{
      let typeOne=this.data.typeOne;
      //处理分类数据格式
      let typeList=[{
        values: typeOne,
        className: 'column1'
      }]

      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        let cateArr=[];
        result.forEach(item => {
          cateArr.push({
            id:item.id,
            text:item.name,
          });
        });
        typeList.push({
          values: cateArr,
          className: 'column2'
        });
      }else{
        typeList.push({
          values: [],
          className: 'column2'
        });
      }

      this.setData({
        typeList: typeList
      })
    }).catch((error) => {
    })
  },

  //显示设备类型选择框
  onShowType(){
    this.setData({ 
			showType: true
    }) 
    //查询一级
    this.findEquipmentCategory();   
  },

  //隐藏设备类型选择框
  onCloseType(){
    this.setData({ 
			showType: false
    })
  },

  //选择设备类型
  onChangeType(event) {
    const { picker, value, index } = event.detail;
    let arr=picker.getValues();
    this.findChildren(arr[0].id);       
  },   

  //确认设备类型
  onConfirmType(e){  
    let arr=e.detail.value;
    console.log(arr);    
    let idArr=[],nameArr=[];

    arr.forEach(item => {
      if(item){
        idArr.push(item.id);
        nameArr.push(item.text);
      }      
		}); 

    this.setData({
      typeIds: idArr,
      typeNames: nameArr.join(' - ')   
    });    
    this.onCloseType();
  },  

  //提交设备类型
  confirmForm(e){
    let param={
      className:'equipment',
      data:{
        id:this.data.equipmentId,
        typeArr:this.data.typeIds
      }
    }

    CommonService.goSave(param).then(res=>{
      if (res.statusCode == 200 && res.data.success == true){
        Toast.success('成功');
        setTimeout(() => {
          //返回
          this.backAndRefresh();
        }, 1000);
      }else{
        Toast.fail('操作失败');
      }
    }).catch((error) => {
      Toast.fail('操作失败');
    })   
  }, 
  
  //返回列表页并刷新数据
  backAndRefresh(){    
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];    
    
    prevPage.getEquipmentType();
    wx.navigateBack();
  },

  //保存日志
  saveLog(obj){
    let business='equipment_category';
    let operateType='submit';
    let dataId=obj.data.id;
    let className=obj.className;
    let data=obj.data;

    CommonService.saveLog(business,operateType,dataId,className,data).then(res=>{       
    }).catch((error) => {                
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})